﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinQ_DTO;

namespace DAO
{
    public class MembershipDAO
    {

        public List<aspnet_Membership> LayDanhSach()
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var list = from c in EFashionDB.aspnet_Memberships
                       select c;
            return list.ToList();
        }

        public aspnet_Membership LayTheoUserId(Guid userId)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var list = from c in EFashionDB.aspnet_Memberships
                       where c.UserId == userId
                       select c;
            return list.Single();

        }


        public void Them(aspnet_Membership temp)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            EFashionDB.aspnet_Memberships.InsertOnSubmit(temp);
            EFashionDB.SubmitChanges();

        }

        public void Xoa(aspnet_Membership temp)
        {

            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var list = from c in EFashionDB.aspnet_Memberships
                       where c.UserId == temp.UserId
                       select c;
            aspnet_Membership Result = list.Single();
            EFashionDB.aspnet_Memberships.DeleteOnSubmit(Result);
            EFashionDB.SubmitChanges();


        }

        public void CapNhat(aspnet_Membership temp)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var list = from c in EFashionDB.aspnet_Memberships
                       where c.UserId == temp.UserId
                       select c;

            foreach (aspnet_Membership u in list)
            {
                u.ApplicationId = temp.ApplicationId;
                u.Password = temp.Password;
                u.PasswordFormat = temp.PasswordFormat;
                u.PasswordSalt = temp.PasswordSalt;
                u.MobilePIN = temp.MobilePIN;
                u.Email = temp.Email;
                u.LoweredEmail = temp.LoweredEmail;
                u.PasswordQuestion = temp.PasswordQuestion;
                u.PasswordAnswer = temp.PasswordAnswer;
                u.IsApproved = temp.IsApproved;
                u.IsLockedOut = temp.IsLockedOut;
                u.CreateDate = temp.CreateDate;
                u.LastLoginDate = temp.LastLoginDate;
                u.LastLockoutDate = temp.LastLockoutDate;
                u.LastPasswordChangedDate = temp.LastPasswordChangedDate;
                u.FailedPasswordAnswerAttemptCount = temp.FailedPasswordAnswerAttemptCount;
                u.FailedPasswordAnswerAttemptWindowStart = temp.FailedPasswordAnswerAttemptWindowStart;
                u.FailedPasswordAttemptCount = temp.FailedPasswordAttemptCount;
                u.FailedPasswordAttemptWindowStart = temp.FailedPasswordAttemptWindowStart;
                u.Comment = temp.Comment;
                u.DiemTichLuy = temp.DiemTichLuy;
                u.Website = temp.Website;
                u.YahooYM = temp.YahooYM;
                u.DiaChi = temp.DiaChi;
                u.DTDD = temp.DTDD;
            }

            EFashionDB.SubmitChanges();

        }
        
 /// <summary>
 //////////////////////////////////CODE BY NMCUONG///////////////////////////////////
 /// </summary>
 /// <param name="Membership"></param>
        public void CapNhatDiemTichLuy(aspnet_Membership Membership)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            aspnet_Membership ms = EFashionDB.aspnet_Memberships.Single(p => p.UserId == Membership.UserId);
            ms.DiemTichLuy = Membership.DiemTichLuy;
            EFashionDB.SubmitChanges();
        }
    }
}
